package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.entity.ClueEntity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

public class ClueDetailDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvDisclose, tvOpen, tvGoDeep, tvTitle;
    private ImageView ivClose, ivClueBack;
    private RelativeLayout rlClueFront;

    private Animation animation;
    private ClueEntity data;
    private String name;

    public ClueDetailDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
//        mLayoutParams.height = AppUtil.getScreenHeight(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_clue_detail, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        tvDisclose = dialogView.findViewById(R.id.tvDisclose);
        tvOpen = dialogView.findViewById(R.id.tvOpen);
        tvGoDeep = dialogView.findViewById(R.id.tvGoDeep);
        ivClueBack = dialogView.findViewById(R.id.ivClueBack);
        ivClose = dialogView.findViewById(R.id.ivClose);
        rlClueFront = dialogView.findViewById(R.id.rlClueFront);

        tvDisclose.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onDisclose(data.getScript_relevant_id());
            }
        });

        tvOpen.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onOpen(data.getScript_relevant_id());
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });
    }

    public void initClueData(ClueEntity clueData, String role) {
        data = clueData;
        Glide.with(mContext).asBitmap().load(clueData.getUrl().get(0).getUrl()).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                Drawable drawable = new BitmapDrawable(resource);
                rlClueFront.setBackground(drawable);
            }
        });

        if (!TextUtils.isEmpty(name)) {
            tvTitle.setText(name);
        }

        if (role.equals("player")) {
            if (clueData.getClue_type() == 0) {//0-公开 1-私有
                tvDisclose.setVisibility(View.GONE);
                tvOpen.setVisibility(View.GONE);
            } else if (clueData.getClue_type() == 1) {
                tvDisclose.setVisibility(View.VISIBLE);
                tvOpen.setVisibility(View.VISIBLE);
            }
        } else if (role.equals("dm")) {
            tvDisclose.setVisibility(View.GONE);
            tvOpen.setVisibility(View.GONE);
        }

        ivClueBack.setVisibility(View.VISIBLE);
        rlClueFront.setVisibility(View.GONE);

        animation = AnimationUtils.loadAnimation(mContext, R.anim.anim_clue_back);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animation = AnimationUtils.loadAnimation(mContext, R.anim.anim_clue_front);
                rlClueFront.setVisibility(View.VISIBLE);
                ivClueBack.setVisibility(View.GONE);
                ivClueBack.startAnimation(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        ivClueBack.startAnimation(animation);
//        if (!data.isIs_look()) {
//            ivClueBack.setVisibility(View.VISIBLE);
//            rlClueFront.setVisibility(View.GONE);
//            animation = AnimationUtils.loadAnimation(mContext, R.anim.anim_clue_back);
//
//            ivClueBack.setOnClickListener(view -> {
//
//                if (null != mCallback) {
//                    mCallback.viewClue(clueData.getId());
//                }
//
//                animation.setAnimationListener(new Animation.AnimationListener() {
//                    @Override
//                    public void onAnimationStart(Animation animation) {
//
//                    }
//
//                    @Override
//                    public void onAnimationEnd(Animation animation) {//当动画结束时需要执行的行为
//
//                        animation = AnimationUtils.loadAnimation(mContext, R.anim.anim_clue_front);
//                        rlClueFront.setVisibility(View.VISIBLE);
//                        ivClueBack.setVisibility(View.GONE);
//                        ivClueBack.startAnimation(animation);
//                    }
//
//                    @Override
//                    public void onAnimationRepeat(Animation animation) {
//
//                    }
//                });
//                ivClueBack.startAnimation(animation);
//            });
//        } else {
//            ivClueBack.setVisibility(View.GONE);
//            rlClueFront.setVisibility(View.VISIBLE);
//        }
    }

    public interface Callback {
        void onDisclose(String clueId);

        void onOpen(String clueId);
    }

}
